package com.cwh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cwh.entity.MenuTree;
import com.cwh.entity.SysPermission;
import com.cwh.entity.SysPermissionExample;
import com.cwh.entity.SysRole;
import com.cwh.entity.SysRolePermission;
import com.cwh.entity.SysRolePermissionExample;
import com.cwh.entity.SysRolePermissionExample.Criteria;
import com.cwh.mapper.SysPermissionMapper;
import com.cwh.mapper.SysPermissionMapperCustom;
import com.cwh.mapper.SysRoleMapper;
import com.cwh.mapper.SysRolePermissionMapper;
import com.cwh.service.SysService;
import com.cwh.utils.UUIDUtils;
import com.cwh.entity.SysPermissionExample;



@Service
public class SysServiceImpl implements SysService{
	
	@Autowired
	private SysPermissionMapperCustom sysPermissionMapperCustom;
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	@Autowired
	private SysRolePermissionMapper sysRolePermissionMapper;
	
	@Autowired
	private SysPermissionMapper sysPermissionMapper;
	
	//查找菜单
	public List<MenuTree> loadMenuTree() {
		
		return sysPermissionMapperCustom.getMenuTree();
	}
	
	//查角色表所有角色
	@Override
	public List<SysRole> findAllSysRole() {
		
		return sysRoleMapper.selectByExample(null);
	}
	
	//根据角色号显示该角色所具有的权限
	@Override
	public List<SysPermission> findPermissionsByRoleId(String roleId) {
		
		return sysPermissionMapperCustom.findPermissionsByRoleId(roleId);
	}
	
	//查询所有菜单及其子菜单 权限
	@Override
	public List<MenuTree> getAllMenuAndPermision() {
		return sysPermissionMapperCustom.getAllMenuAndPermision();
	}
	
	//修改角色对应的权限
	@Override
	public void updateRoleAndPermission(String roleId, String[] permissionIds) {
		//先根据roleId清除sys_role_permission
		SysRolePermissionExample example=new SysRolePermissionExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andSysRoleIdEqualTo(roleId);
		sysRolePermissionMapper.deleteByExample(example);
		
		//根据用户id重新给用户添加对应权限
		for(String s:permissionIds) {
			SysRolePermission record=new SysRolePermission();
			record.setId(UUIDUtils.getRandomNumber());
			record.setSysRoleId(roleId);
			record.setSysPermissionId(s);
			sysRolePermissionMapper.insert(record);
		}
	}
	
	//父目录的
	@Override
	public List<SysPermission> findAllMenus() {
		SysPermissionExample example = new SysPermissionExample();
		com.cwh.entity.SysPermissionExample.Criteria criteria = example.createCriteria();
		//criteria.andTypeLike("%menu%");
		criteria.andTypeEqualTo("menu");
		
		return sysPermissionMapper.selectByExample(example);

	}

	//根据权限表与角色表联查
	@Override
	public List<SysRole> findRolesAndPermissions() {
		return sysPermissionMapperCustom.findRoleAndPermissionList();
	}
	
	//给角色添加所选择的权限
	@Override
	public void addRoleAndPermissions(SysRole sysRole, int[] permissionIds) {
		//添加角色
		sysRoleMapper.insert(sysRole);
		//获取角色id,之前生成的
		String roleId = sysRole.getId();
		//添加角色和权限表关系
		for(int i=0;i<permissionIds.length;i++) {
			SysRolePermission sysRolePermission = new SysRolePermission();
			sysRolePermission.setId(UUIDUtils.getRandomNumber());
			sysRolePermission.setSysRoleId(roleId);
			sysRolePermission.setSysPermissionId(permissionIds[i]+"");
			//在角色管理权限表  插入角色id和权限id
			sysRolePermissionMapper.insert(sysRolePermission);
		}
		
	}
	
	//添加新权限
	@Override
	public void addSysPermission(SysPermission permission) {
		sysPermissionMapper.insert(permission);
		
	}

}
